package com.example.my_model.loginactivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.common.BaseApplication;
import com.example.common.filed.EventBusAction;
import com.example.common.mvvm.view.BaseActivity;
import com.example.my_model.databinding.ActivityLoginBinding;
import com.example.my_model.room.UserDataBase;
import com.example.my_model.room.UserEntity;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;

@Route(path = "/user/LoginActivity")
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginViewModel> implements IUiListener {
    private Tencent tencent;
    private String openid;
    private long expires_time;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBind() {
        return ActivityLoginBinding.inflate(inflater);
    }

    @Override
    public void initView() {
        tencent = Tencent.createInstance("101794421", getApplicationContext());
        tencent.login(this, "all", this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected String pageName() {
        return "用户登录页面";
    }

    @Override
    public void receiveData(Map<String, Object> data) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_LOGIN) {
            Tencent.onActivityResultData(requestCode, resultCode, data, this);
        }
    }

    //授权完成
    @Override
    public void onComplete(Object o) {
        LogUtils.e("成功");
        JSONObject jsonObject = (JSONObject) o;
        Iterator<String> it = jsonObject.keys();
        while (it.hasNext()) {
            String key = it.next();
            //用户登录接口返回值
            if (key.equals("openid")) {
                try {
                    openid = jsonObject.getString("openid");
                    String access_token = jsonObject.getString("access_token");
                    String expires_in = jsonObject.getString("expires_in");
                    tencent.setAccessToken(access_token, expires_in);
                    expires_time = jsonObject.getLong("expires_time");

                    tencent.setOpenId(openid);
                    //配置获取qq用户信息请求
                    UserInfo info = new UserInfo(this, tencent.getQQToken());
                    info.getUserInfo(this);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
        }
        try {
            String figureurl = jsonObject.getString("figureurl");
            String nickname = jsonObject.getString("nickname");
            UserEntity userEntity = new UserEntity();
            userEntity.figureurl = figureurl;
            userEntity.nickname = nickname;
            userEntity.qqOpenId = openid;
            userEntity.expires_time = expires_time;
            vm.requestAddUser(userEntity);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onError(UiError uiError) {

    }


    //取消
    @Override
    public void onCancel() {

    }
}
